//
//  ClubWuKongView.swift
//  77Club
//
//  Created by Apple on 2024/8/28.
//

import UIKit

//MARK: 资讯cell
class ClubWuKongInfoCell: UITableViewCell, RaeNetworkProtocol {

    private let backView = c_make_ex(UIView.self)
        .bgColor(.white).corner(12.0.Scale).end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color("2E3033").font(15.font_medium).line(2).end()
    private let iconImageView = c_make_ex(UIImageView.self)
        .corner(8).mode(.scaleAspectFill).end()
    private let iconShadowView = c_make_ex(UIView.self)
        .bgColor(.black.alpha(0.1)).end()
    private let browseImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("Square_look").end()
    private let commitImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("Square_commit").end()
    let likeImageView = c_make_ex(UIButton.self)
        .image("Square_like").image("Square_liked", state: .selected).end()
    private let browseLabel = c_make_ex(UILabel.self)
        .color("#7B7D80").font(10.font_regular).text("999浏览").end()
    private let commitLikeLabel = c_make_ex(UILabel.self)
        .color("#7B7D80").font(10.font_regular).alignment(.right).end()
    
    var likeBlock: ((_ infoID: String) -> Void)?
    
    var model: ClubWuKongDataModel?{
        didSet{
            guard let md = model?.data else { return }
            
            titleLabel.text = md.title
            iconImageView.loadImage(url: md.image)
            var look = md.look
            if look == "0"{
                look = ""
            }
            browseLabel.text = look + "浏览"
            var pinglun = md.pinglun
            if pinglun == "0"{
                pinglun = ""
            }
            var zan = md.zan
            if zan == "0"{
                zan = ""
            }
            commitLikeLabel.text = pinglun + "评论 · " + zan + "点赞  "
            likeImageView.isSelected = md.is_zan
        }
    }
    
    var infoModel: ClubHomeInfoItemModel?{
        didSet{
            guard let md = infoModel else { return }
            
            iconImageView.loadImage(url: md.image)
            titleLabel.text = md.title
            var look = md.look
            if look == "0"{
                look = ""
            }
            browseLabel.text = look + "浏览"
            var pinglun = md.pinglun
            if pinglun == "0"{
                pinglun = ""
            }
            var zan = md.zan
            if zan == "0"{
                zan = ""
            }
            commitLikeLabel.text = pinglun + "评论 · " + zan + "点赞  "
            likeImageView.isSelected = md.is_zan == 1
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            backView.subviews{
                titleLabel
                iconImageView.subviews{
                    iconShadowView
                }
                browseImageView
                commitImageView
                likeImageView
                browseLabel
                commitLikeLabel
            }
        }
                
        backView.bottom(12.0.Scale).leading(12.0.Scale).trailing(12.0.Scale).top(0)
        titleLabel.leading(16.0.Scale).trailing(16.0.Scale).top(16.0.Scale)
        iconImageView.leading(16.0.Scale).trailing(16.0.Scale).height(155.0.Scale)
        iconImageView.Top == titleLabel.Bottom + 8.0.Scale
        iconShadowView.fillContainer()
        browseImageView.leading(16.0.Scale).size(44.0.Scale)
        browseImageView.Top == iconImageView.Bottom
        likeImageView.trailing(16.0.Scale).size(44.0.Scale)
        likeImageView.CenterY == browseImageView.CenterY
        commitImageView.size(44.0.Scale)
        commitImageView.CenterY == likeImageView.CenterY
        commitImageView.Trailing == likeImageView.Leading
        browseLabel.leading(26.0.Scale).bottom(15.0.Scale)
        browseLabel.Top == browseImageView.Bottom
        commitLikeLabel.trailing(26.0.Scale)
        commitLikeLabel.CenterY == browseLabel.CenterY
        
        likeImageView.addTarget(self, action: #selector(clickLikeButton), for: .touchUpInside)
    }
    
    @objc private func clickLikeButton(){
        
        guard let infoID = model?.data?.id else { return }
        likeBlock?(String(infoID))
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}


class ClubWuKongCateView: UITableViewHeaderFooterView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
         
    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    private var sourceData = [ClubWuKongCateModel]()
    var gid = "" {
        didSet{
            loadData()
        }
    }
    
    var updateCateData: ((_ cateID: Int) -> Void)?
    
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        backgroundColor = "#F7F8FA".toColor
        contentView.backgroundColor = "#F7F8FA".toColor
        
        addSubview(collectionView)
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 15
        layout.minimumInteritemSpacing = 15
        layout.scrollDirection = .horizontal
//        layout.itemSize = CGSize(width: 45.0.Scale, height: 38.0.Scale)
        layout.sectionInset = UIEdgeInsets(top: 0, left: 12.0.Scale, bottom: 0, right: 0)
        
        collectionView.collectionViewLayout = layout
        collectionView.backgroundColor = .clear
        collectionView.register(ClubWuKongCateCell.self, forCellWithReuseIdentifier: ClubWuKongCateCell.description())
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
        
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    private func loadData(){
        
        if gid == "89" {
            sourceData = [
                ClubWuKongCateModel(title: "热聊", type: 0, isSel: true),
                ClubWuKongCateModel(title: "评论", type: 1, isSel: false),
                ClubWuKongCateModel(title: "攻略", type: 2, isSel: false),
//                ClubWuKongCateModel(title: "论坛", type: 3, isSel: false),
                ClubWuKongCateModel(title: "炉石工具", type: 4, isSel: false)
            ]
        }else{
            sourceData = [
                ClubWuKongCateModel(title: "热聊", type: 0, isSel: true),
                ClubWuKongCateModel(title: "评论", type: 1, isSel: false),
                ClubWuKongCateModel(title: "攻略", type: 2, isSel: false),
//                ClubWuKongCateModel(title: "论坛", type: 3, isSel: false),
            ]
        }
        collectionView.reloadData()
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        sourceData.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ClubWuKongCateCell.description(), for: indexPath) as? ClubWuKongCateCell else { return UICollectionViewCell() }
        cell.model = sourceData[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let type = sourceData[indexPath.row].type
        if type == 0 || type == 1 || type == 2 {
            var models = [ClubWuKongCateModel]()
            sourceData.forEach { model in
                var itemModel = model
                itemModel.isSel = false
                models.append(itemModel)
            }
            models[indexPath.row].isSel = true
            sourceData = models
            collectionView.reloadData()
        }
        updateCateData?(type)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        let name = sourceData[indexPath.row].title
        let font = 14.font_medium
        let attributedText = NSAttributedString(string: name, attributes: [NSAttributedString.Key.font: font as Any])

        // 计算文本宽度
        let size = attributedText.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, context: nil).size
        let width = size.width
        
        return CGSize(width: width, height: 38.0.Scale)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
}


class ClubCommitCell: UITableViewCell {
    
    private let bgView = c_make_ex(UIView.self)
        .bgColor(.white).corner(12.0.Scale).end()
    private let iconImageView = c_make_ex(UIImageView.self)
        .corner(20.0.Scale).mode(.scaleAspectFill).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color(.black).font(12.font_semiBold).end()
    private let timeLabel = c_make_ex(UILabel.self)
        .color("#AEB0B3").font(11.font_medium).end()
    private let contentLabel = c_make_ex(UILabel.self)
        .color("#47494D").font(15.font_medium)
        .line(0).end()
    private let subLabel = c_make_ex(UILabel.self)
        .color("#7B7D80").font(10.font_medium).alignment(.right).end()
    private let likeButton = c_make_ex(UIButton.self)
        .image("Square_like").image("Square_liked", state: .selected).end()
    
    var likeCommitBlock: ((_ commitID: String) -> Void)?
    
    var commitModel: ClubInfoCommentListModel? {
        didSet{
            guard let md = commitModel else { return }
            
            iconImageView.loadImage(url: md.user?.face)
            contentLabel.text = md.content
            nameLabel.text = md.user?.user_name
            timeLabel.text = md.time
            var commentCount = 0
            if let down = md.down?.count, down > 0 {
                commentCount = down
            }
            subLabel.text = "\(commentCount)评论 · " + md.zan + "点赞"
            likeButton.isSelected = md.is_zan == 1
        }
    }
    
    var model: ClubGameDetailCommentListModel? {
        didSet{
            guard let md = model else { return }
            
            iconImageView.loadImage(url: md.touxiang)
            contentLabel.text = md.content
            nameLabel.text = md.user_name
            timeLabel.text = md.time
            subLabel.text = md.num + "评论 · " + md.zan + "点赞"
            likeButton.isSelected = md.is_zan
        }
        
    }
    
//    class func cellWithTableView(tableView:UITableView) -> (ClubGameCommentCell) {
//        let reUse:String = "ClubGameCommentCell"
//        
//        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
//        
//        if cell == nil {
//            cell = ClubGameCommentCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
//        }
//        
//        return cell as! ClubGameCommentCell
//    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        contentView.subviews{
            bgView.subviews{
                iconImageView
                nameLabel
                timeLabel
                contentLabel
                subLabel
                likeButton
            }
        }
        
        bgView.leading(12.0.Scale).trailing(12.0.Scale).top(0).bottom(12.0.Scale)
        iconImageView.leading(12.0.Scale).top(16.0.Scale).size(40.0.Scale)
        nameLabel.leading(64.0.Scale).top(20.0.Scale).trailing(15.0.Scale)
        timeLabel.leading(64.0.Scale).top(38.0.Scale).trailing(15.0.Scale)
        contentLabel.leading(64.0.Scale).top(55.0.Scale).trailing(16.0.Scale)
        likeButton.trailing(8.0.Scale).size(44.0.Scale).bottom(0)
        likeButton.Top == contentLabel.Bottom
        subLabel.trailing(56.0.Scale)
        subLabel.CenterY == likeButton.CenterY
        
        likeButton.addTarget(self, action: #selector(clickLikeButton), for: .touchUpInside)
    }
 
    @objc private func clickLikeButton(){
        
        if let ID = model?.id {
            likeCommitBlock?(ID)
        }
        if let ID = commitModel?.id {
            likeCommitBlock?(ID)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
//    @objc func buttonClick(_ button:UIButton) -> Void {
//        photoBroseView.imagesURL = model?.images
//        photoBroseView.frameFormWindow = CGRect(x: screenWidth/2, y: screenHeight/2, width: 0, height: 0)
//        photoBroseView.frameToWindow = CGRect(x: screenWidth/2, y: screenHeight/2, width: 0, height: 0)
//        photoBroseView.currentIndex = button.tag - 9999
//        photoBroseView.show()
//    }
//
//    @objc func zanButtonClick(_ button:UIButton) -> Void {
//        self.zanButtonClick?(model?.id ?? "")
//    }
//
//    @objc func videoButtonClick(_ button:UIButton) -> Void {
//        self.videoPlayClick?(self.model?.video ?? "")
//    }

}
